package crap;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;

import tools.Arrow;
import tools.ColorConversions;
import tools.Derivative;

public class Verifier {
	public static void main(String[] args) throws Exception {
		
		final BufferedImage bi = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("res/4x4diagonal.png"));
		final int w = bi.getWidth(), h = bi.getHeight(), n = w;
		
		int[] pixels = bi.getRGB(0, 0, w, h, new int[w*h], 0, w);
		int[] lum  = ColorConversions.getFastLuminance(pixels, new int[w*h]);
		int[] dx = Derivative.dx(lum, n, n, new int[lum.length]);
		int[] dy = Derivative.dy(lum, n, n, new int[lum.length]);
		
		double[] lengths = Derivative.getLengths(dx, dy, new double[dx.length]);
		double[] angles = Derivative.getAngles(dx, dy, new double[dx.length]);
		
		lum = Derivative.prepareLengths(lengths, lum);
		pixels = ColorConversions.buildRGB(lum, lum, lum, pixels);
		bi.setRGB(0, 0, w, h, pixels, 0, w);
		
		final double[] histo = new double[8];
		double[] ang = new double[8];
		double[] delta = new double[8];
		for (int i = 0; i<ang.length;i++) {
			ang[i] = ((i*2*Math.PI))/8-Math.PI;
			System.out.println(ang[i]);
		}
		for (int y = 0; y<4; y++) {
			for (int x = 0; x<4; x++) {
				double angle = angles[x+y*n];
//				int index = (int)Math.round(8*((angle+Math.PI)/(2*Math.PI))-0.51);
				for (int g = 0; g<histo.length; g++) {
					delta[g] = Math.abs(angle-ang[g]);
				} 
				int minIndex = 0;
				double min = Double.POSITIVE_INFINITY;
				for (int g = 0; g<histo.length; g++) {
					if (delta[g] < min) {
						minIndex = g;
						min = delta[g];					
					}
				} 
				System.out.println("angle: "+angle+"-> index: "+minIndex);
				
				histo[minIndex] += lengths[x+y*n];
			}
		}
		JFrame ff = new JFrame();
		ff.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		final AffineTransform at = new AffineTransform(), rotat0r = new AffineTransform();
		final Arrow arrw = new Arrow();
		final Point2D centre = new Point2D.Double(100,100);
		
		final JComponent canvas = new JComponent() {
			{setPreferredSize(new Dimension(800,800));}
			Line2D line = new Line2D.Double();
			@Override
			public void paint(Graphics g) {
				Graphics2D g2 = (Graphics2D)g;
			    g2.transform(at);
			    g2.drawImage(bi, AffineTransform.getScaleInstance(50, 50), this);

				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			    g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
			    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
			    Point2D point = new Point2D.Double();
			    Point2D centransform = new Point2D.Double();
			    
			    for (int j = 0; j < 1; j++) {
			    	double shiftx = (j%4)*200, shifty = (j/4)*200; 
			    	line.setLine(0, shifty, 800, shifty);
			    	g2.setColor(Color.cyan);
			    	g2.draw(line);
			    	line.setLine(shiftx, 0, shiftx, 800);
			    	g2.draw(line);
			    	
				    for (int i = 0; i < histo.length; i++) {
						point.setLocation(1,0);
						double rot = (i*2*Math.PI)/8.-Math.PI;
//						System.out.println(Math.toDegrees(rot));
						rotat0r.setToRotation(rot);
						rotat0r.transform(point, point);
//						translator.transform(centre, centransform);
						point.setLocation(histo[i]*point.getX()+centre.getX()+shiftx, histo[i]*point.getY()+centre.getY()+shifty);
//						translator.transform(point, point);
						centransform = new Point2D.Double(centre.getX()+shiftx, centre.getY()+shifty);
						if (Double.compare(point.distanceSq(centransform),0)!=0) {
							arrw.setArrow(centransform, point, 3, 10);
						    g2.setColor(Color.yellow);
							g2.fill(arrw);
							g2.setColor(Color.magenta);
							g2.draw(arrw);
						}
						else {
							point.setLocation(1,0);
							rotat0r.transform(point, point);
							point.setLocation(point.getX()+shiftx+centre.getX(), point.getY()+shifty+centre.getY());
							arrw.setArrow(centransform, point, 3, 10);
						    g2.setColor(Color.yellow);
							g2.fill(arrw);
							g2.setColor(Color.magenta);
							g2.draw(arrw);
						}
					}
					
				}
			    
				super.paint(g);
			}
		};
		ff.add(canvas);
		ff.pack();
		ff.setVisible(true);
		
		
	}
}
